import request from '@/utils/request'

// 获取房间列表
export function getRooms(page = 1, limit = 10) {
  return request({
    url: '/rooms',
    method: 'GET',
    params: { page, limit }
  }).catch(error => {
    console.error('获取房间列表失败:', error)
    throw new Error('获取房间列表失败，请稍后重试')
  })
}

// 创建房间
export function createRoom(name, tag, password, description, maxUsers, timeout, userInfo) {
  const data = {
    name,
    tag,
    password,
    description,
    max_users: maxUsers,
    timeout,
    creator_id: String(userInfo?.userId || ''), // 确保是字符串
    creator_name: userInfo?.userName || '' // 添加创建者名称到请求体
  }
  
  // 添加详细的调试信息
  console.log('=== 创建房间调试信息 ===')
  console.log('传入的userInfo:', userInfo)
  console.log('userInfo类型:', typeof userInfo)
  console.log('userInfo.userId:', userInfo?.userId)
  console.log('userInfo.userName:', userInfo?.userName)
  console.log('最终请求数据:', JSON.stringify(data, null, 2)) // 使用JSON.stringify确保显示完整数据
  console.log('========================')
  
  console.log('创建房间时使用的用户信息:', { 
    userId: userInfo?.userId, 
    userName: userInfo?.userName 
  })
  
  return request({
    url: '/rooms',
    method: 'POST',
    data
  }).catch(error => {
    console.error('创建房间失败:', error)
    throw new Error('创建房间失败，请稍后重试')
  })
}

// 加入房间
export function joinRoom(roomId) {
  return request({
    url: `/rooms/${roomId}/join`,
    method: 'POST'
  }).catch(error => {
    console.error('加入房间失败:', error)
    throw new Error('加入房间失败，请稍后重试')
  })
}



// 离开房间
export function leaveRoom(roomId) {
  return request({
    url: `/rooms/${roomId}/leave`,
    method: 'POST'
  }).catch(error => {
    console.error('离开房间失败:', error)
    throw new Error('离开房间失败，请稍后重试')
  })
}

// 删除房间
export function deleteRoom(roomId, userId) {
  
  return request({
    url: `/rooms/${roomId}`,
    method: 'DELETE'
  }).catch(error => {
    console.error('删除房间失败:', error)
    throw new Error('删除房间失败，请稍后重试')
  })
}

// 获取房间信息
export function getRoomInfo(roomId) {
  return request({
    url: `/rooms/${roomId}`,
    method: 'GET'
  }).catch(error => {
    console.error('获取房间信息失败:', error)
    throw new Error('获取房间信息失败，请稍后重试')
  })
}

// 获取消息历史
export function getMessageHistory(roomId, page = 1, limit = 50) {
  return request({
    url: `/rooms/${roomId}/messages`,
    method: 'GET',
    params: { page, limit }
  }).catch(error => {
    console.error('获取消息历史失败:', error)
    throw new Error('获取消息历史失败，请稍后重试')
  })
}

// 发送消息
export function sendMessage(roomId, data) {
  return request({
    url: `/rooms/${roomId}/messages`,
    method: 'POST',
    data: {
      user_id: data.userId || data.user_id,
      user_name: data.username || data.user_name,
      content: data.content
    }
  }).catch(error => {
    console.error('发送消息失败:', error)
    throw new Error('发送消息失败，请稍后重试')
  })
}

// 删除消息
export function deleteMessage(roomId, messageId) {
  const result = request({
    url: `/rooms/${roomId}/messages/${messageId}`,
    method: 'DELETE'
  })
  return result
}

// 编辑消息
export function editMessage(roomId, messageId, data) {
  const result = request({
    url: `/rooms/${roomId}/messages/${messageId}`,
    method: 'PUT',
    data
  })
  return result
}

// 获取房间成员
export function getRoomMembers(roomId) {
  const result = request({
    url: `/rooms/${roomId}/members`,
    method: 'GET'
  })
  return result
}

// 踢出房间成员
export function kickMember(roomId, userId) {
  const result = request({
    url: `/rooms/${roomId}/members/${userId}`,
    method: 'DELETE'
  })
  return result
}

// 获取用户房间列表
export function getUserRooms(userId) {
  const result = request({
    url: `/users/${userId}/rooms`,
    method: 'GET'
  })
  return result
}

// 搜索房间
export function searchRooms(keyword) {
  return request({
    url: '/rooms/search',
    method: 'GET',
    params: { keyword }
  }).catch(error => {
    console.error('搜索房间失败:', error)
    throw new Error('搜索房间失败，请稍后重试')
  })
}

// 获取房间统计数据
export function getRoomStats(roomId) {
  const result = request({
    url: `/rooms/${roomId}/stats`,
    method: 'GET'
  })
  return result
}

// 更新房间设置
export function updateRoomSettings(roomId, data) {
  const result = request({
    url: `/rooms/${roomId}/settings`,
    method: 'PUT',
    data
  })
  return result
}

// 获取房间设置
export function getRoomSettings(roomId) {
  const result = request({
    url: `/rooms/${roomId}/settings`,
    method: 'GET'
  })
  return result
}

// 上传文件
export function uploadFile(roomId, data) {
  const result = request({
    url: `/rooms/${roomId}/files`,
    method: 'POST',
    data
  })
  return result
}

// 获取文件列表
export function getFiles(roomId, page = 1, limit = 20) {
  const result = request({
    url: `/rooms/${roomId}/files`,
    method: 'GET',
    params: { page, limit }
  })
  return result
}